package com.atguigu.crowd.mvc.config;


import com.atguigu.crowd.exception.AccessForbiddenException;
import com.atguigu.crowd.exception.LoginAcctDuplicateKeyException;
import com.atguigu.crowd.exception.LoginAcctDuplicateKeyForUpdateException;
import com.atguigu.crowd.exception.LoginFailedException;
import com.atguigu.crowd.util.CrowdConstant;
import com.atguigu.crowd.util.CrowdUtil;
import com.atguigu.crowd.util.ResultEntity;
import com.google.gson.Gson;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.HttpServletBean;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice
public class CrowdExceptionResolver {

    @ExceptionHandler(value = LoginAcctDuplicateKeyForUpdateException.class)
    public ModelAndView resolveAccessForbiddenException(LoginAcctDuplicateKeyForUpdateException exception, HttpServletRequest request
            , HttpServletResponse response
    ) throws IOException {


        return commonResolve("error", request, response, exception);
    }

    @ExceptionHandler(value = LoginAcctDuplicateKeyException.class)
    public ModelAndView resolveAccessForbiddenException(LoginAcctDuplicateKeyException exception, HttpServletRequest request
            , HttpServletResponse response
    ) throws IOException {


        return commonResolve("admin-add", request, response, exception);
    }

    @ExceptionHandler(value = AccessForbiddenException.class)
    public ModelAndView resolveAccessForbiddenException(AccessForbiddenException exception, HttpServletRequest request
            , HttpServletResponse response
    ) throws IOException {


        return commonResolve("admin-login", request, response, exception);
    }

    @ExceptionHandler(value = LoginFailedException.class)
    public ModelAndView resolveLoginFailedException(LoginFailedException exception, HttpServletRequest request
            , HttpServletResponse response
    ) throws IOException {


        return commonResolve("admin-login", request, response, exception);
    }

    @ExceptionHandler(value = NullPointerException.class)
    public ModelAndView resolveNullPointerException(NullPointerException exception, HttpServletRequest request
            , HttpServletResponse response
    ) throws IOException {


        return commonResolve("error", request, response, exception);
    }

    @ExceptionHandler(value = RuntimeException.class)
    public ModelAndView resolveRuntimeException(RuntimeException exception, HttpServletRequest request
            , HttpServletResponse response
    ) throws IOException {


        return commonResolve("error", request, response, exception);
    }

    @ExceptionHandler(value = ArithmeticException.class)
    public ModelAndView resolveArithmeticException(ArithmeticException exception, HttpServletRequest request
            , HttpServletResponse response
    ) throws IOException {


        return commonResolve("error", request, response, exception);
    }

    private ModelAndView commonResolve(String viewName, HttpServletRequest request, HttpServletResponse response, Exception exception) throws IOException {
        //        判断当前请求
        boolean judge = CrowdUtil.judgeRequestType(request);
//        如果是ajax
        if (judge) {
//            创建ResultEntity对象
            ResultEntity<Object> resultEntity = ResultEntity.errorData(exception.getMessage());

//            创建Gson对象
            Gson gson = new Gson();
//            转为json
            String json = gson.toJson(resultEntity);
            request.setCharacterEncoding("UTF-8");
//            将json字符串响应到前端
            response.getWriter().write(json);

            return null;

        }
        ModelAndView modelAndView = new ModelAndView();
//        如果不是ajax请求
        modelAndView.addObject(CrowdConstant.ATTR_NAME_EXCEPTION, exception);

        modelAndView.setViewName(viewName);


        return modelAndView;
    }
}
